﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TireBias.Models;
using System.IO;
using System.Data;
using WinNemoLib;

namespace TireBias.Controllers
{
    public class TableController : Controller
    {
        //
        // GET: /Table/

        public ActionResult Index()
        {
            return View();
        }
        public void Export(string table)
        {
            //if (table == "mat")
            //{
            //    Table_SEL_MAT();
            //}else
            //    Table_Head_Tail();
                      
        }

        public void Table_Head_Tail(string dFrom,string dTo,string biasno)
        {

            List<ReportModel.Report_DS> list = new List<ReportModel.Report_DS>();
            ReportModel mdSearch = new ReportModel();
            list = mdSearch.GET_REPORT("",dFrom,dTo,biasno);
            if (list.Count() <= 0)
            {
                list = new List<ReportModel.Report_DS>();
            }

            StringWriter sw = new StringWriter();
            if (StrUtil.IsNotEmpty(biasno))
            {
                sw.WriteLine(string.Format("\"{0}\",\"{1}\"", "Biasno : ", biasno));
            }

            if (StrUtil.IsNotEmpty(dFrom) && StrUtil.IsNotEmpty(dTo))
            {
                sw.WriteLine(string.Format("\"{0}\",\"{1}\"", "Date : ", dFrom + "-" + dTo));
            }
            else if (StrUtil.IsNotEmpty(dFrom))
            {
                sw.WriteLine(string.Format("\"{0}\",\"{1}\"", "Date : ", dFrom));
            }

            //First line for column names
            string col = "\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\",\"{10}\",\"{11}\",\"{12}\",\"{13}\",\"{14}\",\"{15}\",\"{16}\",\"{17}\",\"{18}\",\"{19}\",\"{20}\",\"{21}\",\"{22}\",\"{23}\",";
            col = col + "\"{24}\",\"{25}\",\"{26}\",\"{27}\",\"{28}\",\"{29}\",\"{30}\",\"{31}\",\"{32}\",\"{33}\",\"{34}\",\"{35}\",\"{36}\",\"{37}\",\"{38}\",\"{39}\",\"{40}\",\"{41}\",\"{42}\",\"{43}\",\"{44}\",\"{45}\",";
            col = col + "\"{46}\",\"{47}\",\"{48}\",\"{49}\",\"{50}\",\"{51}\",\"{52}\",\"{53}\",\"{54}\",\"{55}\",\"{56}\",\"{57}\",\"{58}\",\"{59}\",\"{60}\",\"{61}\",\"{62}\",\"{63}\",\"{64}\"";
            sw.WriteLine(string.Format(col, "biasno",
            "plandt",   "itemno",  "sitemno",
            "topcrdcd", "sizes",
            "forming",
            "P1",
            "P2",
            "P3",
            "P4",
            "Text",
            "plansts",
            "Uptemp",
            "Uptdat",
            "actflag",
            "actdt",
            "empname",
            "shift",
            "finflag",
            "findat",
            "Tcrdlot",
            "toproll",
            "topexp",
            "toplen",
            "topauthemp",
            "topreatxt",
            "topshtxt",
            "Skpflag",
            "skpdt",
            "skpemp",
            "skpshtxt",
            "smpemp",
            "smpshtxt",
            "Ply1",
            "Ply2",
            "Rundt",
            "finflag",
            "findat",
            "empname",
            "shift",
            "sauthemp",
            "sreatxt",
            "sshtxt",
            "angPLY1",
            "angPLY2", "NFINEmp", "NFINshtxt",
            "side","rollno","cartno","plylot","plyexp","plylen","plyan1","plyan2","plyan3",
            "plywd1","plywd2","plywd3","plyjn1","plyjn2","plyjn3","empname","[update]"
));
            foreach (ReportModel.Report_DS item in list)
            {
                sw.WriteLine(string.Format(col, item._biasno,
                                                item._plandt,
                                                item._itemno,
                                                item._SUB_sitemno,
                                                item._topcrdcd,
                                                item._sizes,
                                                item._forming,
                                                item._P1,
                                                item._P2,
                                                item._P3,
                                                item._P4,
                                                item._Text,
                                                item._plansts,
                                                item._Uptemp,
                                                item._Uptdat,
                                                item._actflag,
                                                item._actdt,
                                                item._empname,
                                                item._shift,
                                                item._finflag,
                                                item._findat,
                                                item._Tcrdlot,
                                                item._toproll,
                                                item._topexp,
                                                item._toplen,
                                                item._topauthemp,
                                                item._topreatxt,
                                                item._topshtxt,
                                                item._Skpflag,
                                                item._skpdt,
                                                item._skpemp,
                                                item._skpshtxt,
                                                item._smpemp,
                                                item._smpshtxt,                                                
                                                item._SUB_Ply1,
                                                item._SUB_Ply2,
                                                item._SUB_Rundt,
                                                item._SUB_finflag,
                                                item._SUB_findat,
                                                item._SUB_empname,
                                                item._SUB_shift,
                                                item._SUB_sauthemp,
                                                item._SUB_sreatxt,
                                                item._SUB_sshtxt,
                                                item._SUB_angPLY1,
                                                item._SUB_angPLY2,
                                                item._NFINEmp,item._NFINshtxt,
                                                item._Side,item._Rollno,item._Cartno,item._PlyLot,item._PlyExp,item._PlyLen,item._Plyan1,item._Plyan2,item._Plyan3,
                                                item._Plywd1,item._Plywd2,item._Plywd3,item._Plyjn1,item._Plyjn2,item._Plyjn3,item._empname,item._Update
                                                ));
            }

            Response.AddHeader("Content-Disposition", "attachment; filename=REPORT.csv");
            Response.ContentType = "text/csv";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("TIS-620");
            Response.Write(sw);
            Response.End();
        }

        public void Table_SEL_MAT(string dFrom ,string dTo,string biasno)
        {

            DataTable dt= new DataTable();

            ReportModel mdSearch = new ReportModel();
            dt = mdSearch.SEL_MATCHANGE(biasno,"","","","","",dFrom,dTo);
            if (dt.Rows.Count <= 0)
            {
                dt = new DataTable();
            }

            StringWriter sw = new StringWriter();

            if (StrUtil.IsNotEmpty(biasno))
            {   sw.WriteLine(  string.Format("\"{0}\",\"{1}\"",  "Biasno : ",biasno ));
            }

            if (StrUtil.IsNotEmpty(dFrom) && StrUtil.IsNotEmpty(dTo))
            {
                sw.WriteLine(  string.Format("\"{0}\",\"{1}\"",  "Date : ",dFrom +"-" +dTo));
            }
            else if (StrUtil.IsNotEmpty(dFrom))
            {
                sw.WriteLine(  string.Format("\"{0}\",\"{1}\"",  "Date : ",dFrom ));
            }

            //First line for column names
            string col = "\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\",\"{10}\",\"{11}\",\"{12}\",\"{13}\",\"{14}\",\"{15}\",\"{16}\",\"{17}\"";
            sw.WriteLine();
            sw.WriteLine(string.Format(col, 
            "Plan", "Topped Cord", "Lot","PLY",
            "Roll no.","Size","NewSize",
            "NewPly","New forming M/c",
            "New Angle",
            "New Width",
            "New Length",
            "New LotNo.",
            "New Expired",
            "New RubberSheet",
            "New Tape",           
            "empname",
            "Update"));
            for (int index = 0; index < dt.Rows.Count; index++ )
            {
                DataRow item = dt.Rows[index];
                sw.WriteLine(string.Format(col, item["plandt"],                                                
                                                item["topcrdcd"],
                                                item["plylot"],
                                                item["side"],
                                                item["rollno"],
                                                item["oldsize"],
                                                item["sizes"],
                                                item["side"],
                                                item["Forming"],
                                                item["angle"],
                                                item["plymin"],
                                                item["plylen"],
                                                item["plylot"],
                                                item["plyexp"],
                                                item["topsheet"],
                                                item["tapesheet"],
                                                item["empname"],                                               
                                               WinNemoLib.DateUtil.ToDisplayDateTime(item["modi_date"])
                                                ));
            }

            Response.AddHeader("Content-Disposition", "attachment; filename=REPORT.csv");
            Response.ContentType = "text/csv";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("TIS-620");
            Response.Write(sw);
            Response.End();
        }


        public JsonResult GetBiasno(string code)
        {
            KANBANPlanModel mdSearch = new KANBANPlanModel();
            KANBANPlanModel.KANBANPlan_DS item = mdSearch.GET_BIASNO(code);
            return Json(new { bias = item._biasno });
        }

        public ActionResult Export_Report(string biasno)
        {
           // ViewBag.Full = "F";
            SessionBag.Current.biasno = biasno;
            return View();
        }

    }
}
